package com.jmp.api.service.impl;

import com.jmp.api.service.CompanyAccountService;
import lombok.extern.slf4j.Slf4j;
import one.stand.enums.LoginEnum;
import one.stand.mapper.CompanyAccountMapperExt;
import one.stand.mapper.CompanyPlatformMapperExt;
import one.stand.model.Company;
import one.stand.model.CompanyAccount;
import one.stand.model.CompanyPlatform;
import one.stand.model.SysDictValue;
import one.stand.service.CompanyBaseService;
import one.stand.service.sys.SysDictValueService;
import one.stand.util.AssertUtil;
import one.stand.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

@Service
@Slf4j
public class CompanyAccountServiceImpl implements CompanyAccountService {
    @Autowired
    private CompanyAccountMapperExt companyAccountMapperExt;
    @Autowired
    private SysDictValueService sysDictValueService;
    @Autowired
    private CompanyBaseService companyBaseService;
    @Autowired
    private CompanyPlatformMapperExt companyPlatformMapperExt;

    @Override
    public void save(Integer companyId, Integer accountType, String appid, String appSecret, String appName) {
        CompanyAccount model = companyAccountMapperExt.selectByCompanyId(companyId, accountType);
        if (null != model) {
            AssertUtil.isFalse(model.getState() == 1, "您已经认证成功，无需再次认证");
            model.setAppId(appid);
            model.setAppSecret(appSecret);
            companyAccountMapperExt.updateByPrimaryKeySelective(model);
        } else {
            Company companyModel = companyBaseService.getById(companyId);
            model = new CompanyAccount();
            model.setAppId(appid);
            model.setAppSecret(appSecret);
            model.setAppName(appName);
            model.setAccountType(accountType);
            model.setCreateTime(DateUtil.currentDate());
            model.setCompanyId(companyId);
            model.setCompanyKey(companyModel.getCompanyKey());
            companyAccountMapperExt.insertSelective(model);
        }
    }

    @Override
    public CompanyAccount auth(String appId) {
        CompanyAccount model = companyAccountMapperExt.selectByAppId(appId);
        AssertUtil.nonNull(model, "授权回调，未查到账号信息。。。。。。。。。。。。。");

        CompanyAccount accountUpdate = new CompanyAccount();
        accountUpdate.setAccountId(model.getAccountId());
        accountUpdate.setState(1);
        companyAccountMapperExt.updateByPrimaryKeySelective(accountUpdate);

        model.setState(1);
        return model;
    }

    @Override
    public CompanyAccount getDefaultIfNull(Integer companyId, LoginEnum loginEnum) {
        if (Objects.isNull(companyId)) {
            return defaultAccount(loginEnum.getAccountType());
        }
        CompanyAccount companyAccountModel = get(companyId, loginEnum);
        if (!LoginEnum.WX_PAY.equals(loginEnum) && Objects.isNull(companyAccountModel)) {
            companyAccountModel = defaultAccount(loginEnum.getAccountType());
        }
        return companyAccountModel;
    }

    @Override
    public CompanyAccount get(Integer companyId, LoginEnum loginEnum) {
        log.info("companyId:{},loginEnum:{}", companyId, loginEnum);
        CompanyPlatform companyPlatform = companyPlatformMapperExt.selectPlatformByCompanyId(companyId);
        if (Objects.nonNull(companyPlatform)) {
            return companyAccountMapperExt.selectByCompanyKey(companyPlatform.getCompanyKey(), loginEnum.getAccountType());
        }
        return null;
    }

    @Override
    public CompanyAccount getDefaultIfNull(String companyKey, LoginEnum loginEnum) {
        if (Objects.isNull(companyKey)) {
            return defaultAccount(loginEnum.getAccountType());
        }
        CompanyAccount companyAccountModel = companyAccountMapperExt.selectByCompanyKey(companyKey, loginEnum.getAccountType());
        if (Objects.isNull(companyAccountModel)) {
            companyAccountModel = defaultAccount(loginEnum.getAccountType());
        }
        return companyAccountModel;
    }

    @Override
    public CompanyAccount getByAppId(String appId) {
        CompanyAccount account = companyAccountMapperExt.selectByAppId(appId);
        return Objects.nonNull(account) && account.getState() == 1 ? account : account;
    }

    @Override
    public CompanyAccount getByAppId2(String appId) {
        CompanyAccount account = companyAccountMapperExt.selectByAppId2(appId);
        return Objects.nonNull(account) && account.getState() == 1 ? account : account;
    }

    /**
     * 获取默认账号
     */
    private CompanyAccount defaultAccount(Integer type) {
        SysDictValue sysDictValue = sysDictValueService.get(SysDictValueService.Type.default_company_key, SysDictValueService.TypeName.default_company_key);
        return companyAccountMapperExt.selectByCompanyKey(sysDictValue.getValue(), type);
    }
}
